package com.kx.center.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.kx.center.common.CustomHashMap;
import com.kx.center.entity.Goods;
import com.kx.center.entity.Order;
import com.kx.center.entity.OrderDetail;
import com.kx.center.entity.Stockin;
import com.kx.center.entity.StockinDetail;
import com.kx.center.entity.query.QueryOrder;
import com.kx.center.entity.query.QueryResult;
import com.kx.center.mappers.GoodsMapper;
import com.kx.center.mappers.OrderMapper;
import com.kx.center.mappers.StockinMapper;
import com.kx.center.service.OrderService;
import com.kx.center.util.SerialNumber;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	OrderMapper mapper;
	
	@Autowired
	StockinMapper stockinMapper;
	
	@Autowired
	GoodsMapper goodsMapper;
	
	@Override
	public void insertOrder(Order entity) {
		mapper.insertOrder(entity);
	}
	
	@Override
	public void insertOrderDetail(OrderDetail entity) {
		mapper.insertOrderDetail(entity);
	}

	@Override
	public void updateOrder(Order entity) {
		mapper.updateOrder(entity);
	}

	@Override
	public void updateOrderDetail(OrderDetail entity) {
		mapper.updateOrderDetail(entity);
	}

	@Override
	public List<Order> findOrderByStoreId(Integer storeId) {
		return mapper.findOrderByStoreId(storeId);
	}

	@Override
	public List<OrderDetail> findOrderDetailByOId(Integer oId) {
		List<OrderDetail> result = new ArrayList<OrderDetail>();
		List<OrderDetail> list = mapper.findOrderDetailByOId(oId);
		
		for(OrderDetail item : list) {
			if(item.getQuantity() > 0)
				result.add(item);
		}
		
		return result;
	}

	@Override
	public Order findOrderById(Integer orderId) {
		return mapper.findOrderById(orderId);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void confirm(Order entity) {
		mapper.updateOrder(entity);
		
		// 确认订货单, 自动创建收货单
		List<OrderDetail> details = findOrderDetailByOId(entity.getId());
		
		String stockin_sn = SerialNumber.getSN(SerialNumber.DAILY_COUNTING, entity.getStoreId());
		
		Stockin stockin = new Stockin();
		stockin.setSn(stockin_sn);
		stockin.setStatus(0);
		stockin.setStoreId(entity.getStoreId());
		stockin.setOrderId(entity.getId());
		
		stockinMapper.insertStockin(stockin);
		
		int sid = stockin.getId();
		
		for(OrderDetail item : details) {
			StockinDetail detail = new StockinDetail();
			detail.setSid(sid);
			detail.setSn(stockin_sn);
			detail.setGoodsId(item.getGoodsId());
			detail.setGoodsSku(item.getGoodsSku());
			detail.setMemo(null);
			detail.setOrderQuantity(item.getQuantity());
			detail.setQuantity(0);
			detail.setLoss(0);
			detail.setStoreId(entity.getStoreId());
			
			stockinMapper.insertStockinDetail(detail);
		}
		
		stockin.setStatus(1);
		stockinMapper.updateStockin(stockin);

		entity.setStockinId(stockin.getId());
		mapper.updateOrder(entity);
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void saveOrder(Integer storeId, Order order, List<HashMap<String, Object>> json) {

		if(order != null) {
			// 修改
			List<Goods> goods = goodsMapper.findGoodsByStoreId(order.getStoreId());
			
			HashMap<Integer, String> skuMapping = new HashMap<Integer, String>();
			
			for(Goods item : goods) {
				skuMapping.put(item.getId(), item.getSku());
			}
			
			Integer orderId = order.getId();
			
			for(HashMap<String, Object> map : json) {
				CustomHashMap<String, Object> item = CustomHashMap.convert(map);
				Integer goodsId = item.getInteger("goods_id");
				
				OrderDetail entity = findOrderDetail(orderId, goodsId);
				
				if(entity == null) {
					entity = new OrderDetail();
					entity.setOid(orderId);
					entity.setSn(order.getSn());
					entity.setGoodsId(goodsId);
					entity.setGoodsSku(skuMapping.get(goodsId));
					entity.setQuantity(item.getInteger("quantity"));
					insertOrderDetail(entity);
				} else {
					entity.setQuantity(item.getInteger("quantity"));
					updateOrderDetail(entity);
				}
			}
			updateOrder(order);
		} else {
			// 新增
			String sn = SerialNumber.getSN(SerialNumber.DAILY_COUNTING, storeId);
			
			order = new Order();
			order.setSn(sn);
			order.setStatus(0);
			order.setStoreId(storeId);
			
			insertOrder(order);
			
			Integer oid = order.getId();
			
			for(HashMap<String, Object> map : json) {
				CustomHashMap<String, Object> item = CustomHashMap.convert(map);
				OrderDetail entity = new OrderDetail();
				entity.setOid(oid);
				entity.setSn(sn);
				entity.setGoodsId(item.getInteger("goods_id"));
				entity.setGoodsSku(item.getString("goods_sku"));
				entity.setQuantity(item.getInteger("quantity"));
				
				// TODO 数据校验
				insertOrderDetail(entity);
			}
			order.setStatus(1);
			updateOrder(order);
		}
	}

	@Override
	public QueryResult queryOrder(QueryOrder query) {
		QueryResult result = new QueryResult();
		result.setPage(query.getPage());
		result.setPageSize(query.getPageSize());
		result.setTotalRecord(mapper.queryOrderCount(query));
		result.setData(mapper.queryOrder(query));
		return result;
	}

	@Override
	public Order findOrderBySN(String sn) {
		return mapper.findOrderBySN(sn);
	}

	@Override
	public OrderDetail findOrderDetail(Integer orderId, Integer goodsId) {
		return mapper.findOrderDetail(orderId, goodsId);
	}
	
}
